package com.bestcem.xm.survey.convert.grpc;

import com.bestcem.bp.xm.user.grpc.v1.services.Organization;
import com.bestcem.bp.xm.user.grpc.v1.services.User;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import com.bestcem.xm.member.grpc.v1.common.ExtendFieldValue;
import com.bestcem.xm.member.grpc.v1.common.LabelValue;
import com.bestcem.xm.member.grpc.v1.common.Member;
import com.bestcem.xm.openplatform.grpc.v1.services.ExtParamDtype;
import com.bestcem.xm.openplatform.grpc.v1.services.ExtParamEnumManageStatus;
import com.bestcem.xm.openplatform.grpc.v1.services.ExtParamStatus;
import com.bestcem.xm.qdes.grpc.v1.services.NameList;
import com.bestcem.xm.qdes.grpc.v1.services.QStruct;
import com.bestcem.xm.survey.entity.*;
import com.bestcem.xm.survey.grpc.client.dto.*;
import com.bestcem.xm.survey.grpc.v1.services.*;
import org.mapstruct.*;

import java.util.List;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING,
        collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
        uses = {BaseConvertMapper.class},
        imports = {CustomLinkDeliverReqMethod.class, CustomLinkDeliverLinkAgingType.class, CustomLinkDeliverParamOnlyAgingType.class,
                DeliverExtparamsSource.class, DeliverExtparamsStatus.class, DeliverExtparamsExtparamType.class,
                DeliverExtparamsDeliverExtparamType.class, ExtParamDtype.class, ExtParamStatus.class, ExtParamEnumManageStatus.class,
                RespondentDataStatus.class, RespondentStatus.class, RespondentSource.class, RespondentClientType.class, ColumnQtype.class,
                ColumnStatus.class, ColumnCtype.class, RespondentExtraSimilarStatus.class})
public interface SurveyGrpcConvertMapper {

    @Mapping(target = "status", expression = "java(organization.getStatusValue())")
    @Mapping(target = "source", expression = "java(organization.getSourceValue())")
    @Mapping(target = "footer", expression = "java(organization.getFooterValue())")
    @Mapping(target = "deliverCheck", source = "deliverAudit")
    OrgInfoDto toOrgInfoDto(Organization organization);

    @Mapping(target = "status", expression = "java(project.getStatus().getNumber())")
    @Mapping(target = "ptype", expression = "java(project.getPtype().getNumber())")
    ProjectDto toProjectDto(com.bestcem.xm.qdes.grpc.v1.services.Project project);

    ProjectDto toProjectDto(com.bestcem.xm.qdes.grpc.v1.services.QdesProject project);

    @Mapping(target = "status", expression = "java(user.getStatus().getNumber())")
    @Mapping(target = "edition", expression = "java(user.getEdition().getNumber())")
    @Mapping(target = "userIndex", expression = "java(user.getIndex().getNumber())")
    UserDto toUserDto(User user);

    @Named("deliverExtStatus")
    default ExtParamStatus deliverExtStatus(Integer status) {
        if (status == null) {
            return null;
        }
        return ExtParamStatus.forNumber(status);
    }

    @Named("deliverExtParamDtype")
    default ExtParamDtype deliverExtParamDtype(Integer dtype) {
        if (dtype == null) {
            return null;
        }
        return ExtParamDtype.forNumber(dtype);
    }

    @Mapping(target = "dtype", qualifiedByName = "deliverExtParamDtype")
    @Mapping(target = "status", qualifiedByName = "deliverExtStatus")
    com.bestcem.xm.openplatform.grpc.v1.services.ExtParam toGrpcExtParam(ExtParamDto dto);

    @Named("extParamStatus")
    default Integer extParamStatus(ExtParamStatus item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Named("extDtype")
    default Integer extDtype(ExtParamDtype item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Mapping(target = "dtype", qualifiedByName = "extDtype")
    @Mapping(target = "status", qualifiedByName = "extParamStatus")
    ExtParamDto toExtParamDto(com.bestcem.xm.openplatform.grpc.v1.services.ExtParam item);

    List<ExtParamDto> toExtParamDtoList(List<com.bestcem.xm.openplatform.grpc.v1.services.ExtParam> item);


    @Named("extParamEnumManageStatus")
    default ExtParamEnumManageStatus extParamEnumManageStatus(Integer status) {
        if (status == null) {
            return null;
        }
        return ExtParamEnumManageStatus.forNumber(status);
    }

    @Mapping(target = "status", qualifiedByName = "extParamEnumManageStatus")
    com.bestcem.xm.openplatform.grpc.v1.services.ExtParamEnumManage toGrpcExtParamEnumManage(ExtParamEnumManageDto dto);


    @Named("extParamEnumManageDtoStatus")
    default Integer extParamEnumManageDtoStatus(ExtParamEnumManageStatus item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Mapping(target = "status", qualifiedByName = "extParamEnumManageDtoStatus")
    ExtParamEnumManageDto toExtParamEnumManageDto(com.bestcem.xm.openplatform.grpc.v1.services.ExtParamEnumManage item);

    List<ExtParamEnumManageDto> toExtParamEnumManageDtoList(List<com.bestcem.xm.openplatform.grpc.v1.services.ExtParamEnumManage> item);


    @Named("delayStatus")
    default Integer delayStatus(DeliverExtparamsStatus item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Mapping(target = "deliverExtparamType", expression = "java(item.getDeliverExtparamType().getNumber())")
    @Mapping(target = "source", expression = "java(item.getSourceValue())")
    @Mapping(target = "extparamType", expression = "java(item.getExtparamType().getNumber())")
    @Mapping(target = "status", qualifiedByName = "delayStatus")
    DeliverExtParamsDto toDeliverExtParamsDto(com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams item);

    List<DeliverExtParamsDto> toDeliverExtParamsDtoList(List<com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams> items);

    @Named("deliverExtparamType")
    default DeliverExtparamsDeliverExtparamType deliverExtparamType(Integer delayFailedTtype) {
        if (delayFailedTtype == null) {
            return null;
        }
        return DeliverExtparamsDeliverExtparamType.forNumber(delayFailedTtype);
    }

    @Named("extparamsSource")
    default DeliverExtparamsSource extparamsSource(Integer extparamsSource) {
        if (extparamsSource == null) {
            return null;
        }
        return DeliverExtparamsSource.forNumber(extparamsSource);
    }

    @Named("extparamsExtparamType")
    default DeliverExtparamsExtparamType extparamsExtparamType(Integer extparamsSource) {
        if (extparamsSource == null) {
            return null;
        }
        return DeliverExtparamsExtparamType.forNumber(extparamsSource);
    }

    @Named("delayStatusDto")
    default DeliverExtparamsStatus delayStatusDto(Integer item) {
        if (item == null) {
            return null;
        }
        return DeliverExtparamsStatus.forNumber(item);
    }

    @Mapping(target = "deliverExtparamType", source = "deliverExtparamType", qualifiedByName = "deliverExtparamType")
    @Mapping(target = "source", source = "source", qualifiedByName = "extparamsSource")
    @Mapping(target = "extparamType", source = "extparamType", qualifiedByName = "extparamsExtparamType")
    @Mapping(target = "status", source = "status", qualifiedByName = "delayStatusDto")
    com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams toGrpcDeliverExtparams(DeliverExtParamsDto item);

    List<com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams> toGrpcDeliverExtparamsList(List<DeliverExtParamsDto> items);


    default CustomLinkDeliver toGrpcCustomLinkDeliver(CustomLinkDeliverDto item) {
        if (item == null) {
            return null;
        }

        CustomLinkDeliver.Builder customLinkDeliver = CustomLinkDeliver.newBuilder();

        //deliver_id survey_id param_only_aging_type 3个参数
        if (item.getSurveyId() != null) {
            customLinkDeliver.setSurveyId(item.getSurveyId());
        }
        if (item.getDeliverId() != null) {
            customLinkDeliver.setDeliverId(item.getDeliverId());
        }
        if (item.getParamOnlyAgingType() != null) {
            customLinkDeliver.setParamOnlyAgingType(CustomLinkDeliverParamOnlyAgingType.forNumber(item.getParamOnlyAgingType()));
        }

        return customLinkDeliver.build();
    }

    @Mapping(target = "reqMethod", expression = "java(item.getReqMethodValue())")
    @Mapping(target = "linkAgingType", expression = "java(item.getLinkAgingTypeValue())")
    @Mapping(target = "paramOnlyAgingType", expression = "java(item.getParamOnlyAgingTypeValue())")
    CustomLinkDeliverDto toCustomLinkDeliverDto(com.bestcem.xm.deliver.grpc.v1.services.CustomLinkDeliver item);

    List<CustomLinkDeliverDto> toCustomLinkDeliverDtoList(List<com.bestcem.xm.deliver.grpc.v1.services.CustomLinkDeliver> items);

    @Named("delayFailedttype")
    default Integer delayFailedttype(DeliverDelayFailedTtype item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Named("awardUseType")
    default Integer awardUseType(DeliverAwardUseType item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Mapping(target = "ttype", expression = "java(item.getTtypeValue())")
    @Mapping(target = "status", expression = "java(item.getStatusValue())")
    @Mapping(target = "channel", expression = "java(item.getChannelValue())")
    @Mapping(target = "scenes", expression = "java(item.getScenesValue())")
    //@Mapping(target = "delayFailedTtype", expression = "java(item.getDelayFailedttypeValue())")
    @Mapping(target = "delayFailedTtype", source = "delayFailedttype", qualifiedByName = "delayFailedttype")
    @Mapping(target = "delayFailedTime", source = "delayFailedtime")
    //@Mapping(target = "awardUseType", expression = "java(item.getAwardUseTypeValue())")
    @Mapping(target = "awardUseType", source = "awardUseType", qualifiedByName = "awardUseType")
    DeliverDto toDeliverDto(com.bestcem.xm.deliver.grpc.v1.services.Deliver item);

    PushUserInfoDto toPushUserInfoDto(com.bestcem.xm.push.grpc.v1.services.UserInfo item);

    @Mapping(target = "fieldId", source = "id")
    @Mapping(target = "type", expression = "java(item.getType().getNumber())")
    MemberDto.ExtendFieldValueDto toExtendFieldValueDto(ExtendFieldValue item);


    MemberDto.LabelValueDto toLabelValueDto(LabelValue item);

    @Mapping(target = "mobile", source = "phone")
    @Mapping(target = "phone", source = "phone")
    @Mapping(target = "store", source = "storeId")
    @Mapping(target = "source", expression = "java(member.getSource().getNumber())")
    @Mapping(target = "status", expression = "java(member.getStatus().getNumber())")
    @Mapping(target = "memberType", expression = "java(member.getMemberType().getNumber())")
    @Mapping(target = "employeeStatus", expression = "java(member.getEmployeeStatus().getNumber())")
    @Mapping(target = "memberDeliveryStatus", expression = "java(member.getMemberDeliveryStatus().getNumber())")
    @Mapping(target = "subscribeStatus", expression = "java(member.getSubscribeStatus().getNumber())")
    MemberDto toMemberDto(Member member);

    @Mapping(target = "status", expression = "java(qstruct.getStatusValue())")
    QstructsDto toQstructsDto(QStruct qstruct);

    @Mapping(target = "ntype", expression = "java(item.getNtypeValue())")
    QdesNameListDto toQdesNameListDto(NameList item);

    @Mapping(target = "status", expression = "java(item.getStatus().getNumber())")
    @Mapping(target = "rspdStatus", expression = "java(item.getRspdStatus().getNumber())")
    DeliverWechatmpRecordDto toDeliverWechatmpRecordDto(WechatMPRecord item);

    @Mapping(target = "dataStatus", expression = "java(RespondentDataStatus.forNumber(surveyRespondent.getDataStatus()))")
    @Mapping(target = "status", expression = "java(RespondentStatus.forNumber(surveyRespondent.getStatus()))")
    @Mapping(target = "source", expression = "java(RespondentSource.forNumber(surveyRespondent.getSource()))")
    @Mapping(target = "clientType", expression = "java(RespondentClientType.forNumber(surveyRespondent.getClientType()))")
    @Mapping(target = "wxInfo", expression = "java(com.alibaba.fastjson.JSONArray.toJSONString(surveyRespondent.getWxInfos()))")
    Respondent toRespondent(SurveyRespondent surveyRespondent);

    /*@Mapping(target = "dataStatus", expression = "java(respondent.getDataStatus().getNumber())")
    @Mapping(target = "status", expression = "java(respondent.getStatus().getNumber())")
    @Mapping(target = "source", expression = "java(respondent.getSource().getNumber())")
    @Mapping(target = "clientType", expression = "java(respondent.getClientType().getNumber())")
    @Mapping(target = "wxInfo", expression = "java(null)")
    SurveyRespondent toSurveyRespondent(Respondent respondent);*/

    SurveyQuotaCondition toSurveyQuotaCondition(QuotaCondition condition);

    SurveyQuotaConditionQgidList toSurveyQuotaConditionQgidList(QuotaConditionQgidList condition);

    SurveyQuotaCheckItem toSurveyQuotaCheckItem(QuotaCheckItem checkItem);

    SurveyQuotaCheckItemOptionCombies toSurveyQuotaCheckItemOptionCombies(QuotaCheckItemOptionCombies optionCombies);

    SurveyQuotaCheckItemOptionList toSurveyQuotaCheckItemOptionList(QuotaCheckItemOptionList optionList);

    @Mapping(target = "qtype", expression = "java(ColumnQtype.forNumber(column.getQtype()))")
    @Mapping(target = "status", expression = "java(ColumnStatus.forNumber(column.getStatus()))")
    @Mapping(target = "ctype", expression = "java(ColumnCtype.forNumber(column.getCtype()))")
    Column toColumn(SurveyColumn column);

    QuotaCheckItem toQuotaCheckItem(SurveyQuotaCheckItem surveyQuotaCheckItem);

    RespondentData toRespondentData(SurveyRespondentData surveyRespondentData);

    @Mapping(target = "similarStatus", expression = "java(RespondentExtraSimilarStatus.forNumber(surveyRespondentExtra.getSimilarStatus()))")
    RespondentExtra toRespondentExtra(SurveyRespondentExtra surveyRespondentExtra);

    ExtParameter toExtParameter(SurveyExtParameter item);

    List<ExtParameter> toExtParameterList(List<SurveyExtParameter> items);
}
